package com.huawei.M01.iterator.M08;

/**
 * 122. 买卖股票的最佳时机 II
 *
 * @author 11
 */
public class MaxProfit {
    public int maxProfit(int[] prices) {
        int res = 0;
        int left = prices[0];
        for (int i = 0; i < prices.length; i++) {
            if (i + 1 < prices.length && prices[i] < prices[i + 1]) {
                left = Math.min(left, prices[i]);
            }else if (i + 1 < prices.length && prices[i] > prices[i + 1]) {
                if (left < prices[i]) {
                    res+= prices[i] - left;
                }
                left = prices[i];
            }
        }
        if (left < prices[prices.length - 1]) {
            res += prices[prices.length - 1] - left;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new MaxProfit().maxProfit(new int[]{7,6,4,3,1}));

    }
}
